from django.db import models


from sportsground.utils.baseModels import BaseModels, IntegerChoices


class Vip(BaseModels):
    class Meta():
        db_table = 'venue_vip'
        verbose_name = 'vip'
        verbose_name_plural = verbose_name

    class Suit(IntegerChoices):
        # 订场 商品 租赁
        Rent = 1, "租赁"  # 001
        Goods = 2, "商品"  # 010
        GoodsAndRend = 3, "商品 租赁"  # 011
        Yard = 4, "订场"  # 100
        YardAndRent = 5, "订场 租赁"  # 101
        YardAndGoods = 6, "订场 商品"  # 110
        YardAndGoodsAndRend = 7, "订场 商品 租赁"  # 111

    name = models.CharField(max_length=20, verbose_name="名称")
    venue = models.ForeignKey(to="Venue.Venue", on_delete=models.CASCADE, verbose_name="对应馆场")
    venueYard = models.ForeignKey(to="VenueSportYard", null=True, on_delete=models.CASCADE, verbose_name="对应馆场场地")
    suit = models.IntegerField(choices=Suit.choices, verbose_name="适用类型")

    duration = models.IntegerField(verbose_name="有效期间")
    rule = models.CharField(max_length=200, verbose_name="规则")
    message = models.CharField(max_length=200, verbose_name="价值描述")
    price = models.IntegerField(verbose_name="购买价格")
    value = models.IntegerField(verbose_name="使用价值")

    @classmethod
    def create(cls, venue, suit, name, duration, price, value, rule='', message='', yard=None):
        """ 创建 """
        vip = cls(name=name, venue=venue, venueYard=yard, suit=suit, duration=duration,
                  rule=rule, message=message, price=price, value=value)
        vip.save()
        return vip

    def canPay(self,type):
        """ 支付类型 """
        from User.models import Order
        if type == Order.OrderType.Yard and self.suit in [4,5,6,7]:
            return True
        elif type == Order.OrderType.Goods and self.suit in [2,3,6,7]:
            return True
        elif type == Order.OrderType.Rent and self.suit in [1,3,5,7]:
            return True
        return False

    def Info(self):
        return {
            "id": self.id,
            "name": self.name,
            "yard": self.venueYard.Info(),
            "suit": self.suit,
            "duration": self.duration,
            "rule": self.rule,
            "message": self.message,
            "price": self.price,
            "value": self.value
        }
